<template>
  <div>
    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column type="index" label="序号" width="60"> </el-table-column>
      <template v-for="item in tableHead">
        <el-table-column
          v-if="item.columnType === 'slot'"
          :prop="item.field"
          :label="item.label"
          :key="item.field"
          :width="item.width"
          :align="align"
          :fixed="item.fixed"
        >
          <template slot-scope="scope">
            <slot :name="item.slotName" :elscope="scope" />
          </template>
        </el-table-column>
        <el-table-column
          v-else
          :prop="item.field"
          :label="item.label"
          :key="item.field"
          :width="item.width"
          :align="align"
          :fixed="item.fixed"
        ></el-table-column>
      </template>
    </el-table>

    <el-pagination
      class="flexend"
      style="margin-top: 20px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import Request from "@/util/request";
export default {
  name: "pg-el-table-url",
  data: function () {
    return {
      tableData: [],
      currentPage: 1,
      total: 0,
      pageSize: 10,
    };
  },
  props: {
    qyeryData: { type: Object },
    tableUrl: { type: String },
    tableHeadConfig: { type: Array },
    align: {
      type: String,
      default: "left",
    },
  },
  methods: {
    async query(currentPage) {
      currentPage = currentPage == null ? 1 : currentPage;
      var query = { pages: currentPage, rows: this.pageSize };

      for (let key in this.qyeryData) {
        query[key] = this.qyeryData[key];
      }

      var res = await Request({
        url: this.tableUrl,
        method: "post",
        data: query,
      });

      if (res.isSuccess) {
        var resData = res.data;
        this.tableData = resData.data;

        this.currentPage = resData.currentPage;
        this.total = resData.total;
        this.pageSize = resData.rows;
      }
    },
    async handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      await this.query();
    },
    async handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      await this.query(val);
    },
  },
  computed: {
    tableHead() {
      return this.tableHeadConfig;
    },
  },

  async created() {
    await this.query();
  },
};
</script>

<style>
</style>